package com.lc.ibps.platform.rest.system;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lc.ibps.api.common.identity.service.IdentityService;
import com.lc.ibps.base.core.util.AppUtil;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.web.controller.BaseApiController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RequestMapping(value = "/api/webapi/identityService")
@Api(value = "/identityService", tags = "系统管理-流水号服务")
@RestController
public class IdentityController extends BaseApiController {

	private IdentityService identityService;

	public IdentityController() {
		identityService = AppUtil.getBean(IdentityService.class);
	}

	@ApiOperation(value = "获取流水号值", notes = "流水号服务")
	@RequestMapping(value = "/getNextIdByAlias", method = RequestMethod.GET)
	public APIResult<Map<String, String>> getNextIdByAlias(
			@ApiParam(value = "别名", required = true) @RequestParam(name = "alias", required = true) String alias) {
		APIResult<Map<String, String>> result = new APIResult<>();

		try {
			// TODO 这个方法要修改，逻辑判断放在这层
			identityService.isExistByAlias(alias);
			String nextId = identityService.genNextNo(alias);
			Map<String, String> map = new HashMap<>();
			map.put("id", nextId);
			result.setData(map);
			result.setMessage("获取流水号值成功！");
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "生成编号错误,请联系管理员！", e);
		}

		return result;
	}

}
